﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.DirectoryServices.AccountManagement;
using System.Web.Configuration;
using System.ServiceModel;

namespace TripVoyage
{
    public class Comn
    {
        public static string AClerkDisplay(string Acnt)
        {
            string sResult = string.Empty;

            if (Acnt == "" || Acnt == null)
                sResult = "";
            else
            {
                try
                {
                    sResult = Comn.Groupersons(WebConfigurationManager.AppSettings["AClerk"])[Acnt];
                }
                catch (Exception)
                {
                    sResult = "";
                }
            }
            return sResult;
        }

        public static Dictionary<string, string> Groupersons(string gp)
        {
            Dictionary<string, string> usrs = new Dictionary<string, string>();
            PrincipalContext ptx = null;
            GroupPrincipal grp = null;
            try
            {
                ptx = new PrincipalContext(ContextType.Domain);
                grp = GroupPrincipal.FindByIdentity(ptx, gp);
            }
            catch (SystemException)
            {
                ptx = new PrincipalContext(ContextType.Machine);
                grp = GroupPrincipal.FindByIdentity(ptx, gp);
            }
            if (grp != null)
            {
                foreach (Principal p in grp.Members)
                {
                    usrs.Add(p.SamAccountName, p.DisplayName);
                }
                grp.Dispose();
            }
            if (ptx != null) ptx.Dispose();
            return usrs;
        }
        public static bool IsCredit(string gRole)
        {
            WindowsPrincipal wp = new WindowsPrincipal(ServiceSecurityContext.Current.WindowsIdentity);
            return wp.IsInRole(WebConfigurationManager.AppSettings[gRole]);
        }
        public static string Visitor()
        {
            WindowsPrincipal wp = new WindowsPrincipal(ServiceSecurityContext.Current.WindowsIdentity);
            return wp.Identity.Name.Split(new char[] { '\\' })[1];
        }
    }
}
